// pages/payment/payment.js
const util = require("../../utils/util.js")
const r = require("../../utils/interface.js")
const p = require("../../utils/ment.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 订单编号
    orderNumber: '',
    amount: 0
  },
  // 支付
  async pay() {

    let openid = wx.getStorageSync('openid') || null
    // 获取code

    let {
      code
    } = await util.login().catch(err => err)


    // 获取open
    if (!openid) {
      let open = await r.request({
        url: "openid.php",
        data: {
          code
        }
      }).catch(err => err)
      openid = open.data.openid
      //  存储本地
      wx.setStorageSync('openid', openid)
    }


    // 预支付
    let ment = await r.request({
      url: "jsapi.php",
      method: "POST",
      data: {
        openid,
        body: "支付测试",
        out_trade_no: this.data.orderNumber,
        total_fee: this.data.amount
      }
    })
    let data = ment.data
    // 调用支付
    let result = await p.Payment({
      ...data
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  // 接收传递过来的金额和订单号
  onLoad(e) {
    console.log(e);
    this.data.amount = e.amount
    this.data.orderNumber = e.orderNumber
    this.setData({
      amount: this.data.amount,
      orderNumber: this.data.orderNumber
    })
  },












})